﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Property.Is.In
{
    /// <summary>
    /// Represents a contract that adheres to <b>property is in range</b> semantic.
    /// </summary>
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Property)]
    [ProvideAspectRole(Roles.Conditional)]
    [Serializable]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class RangeAttribute : ComparingContractBase
    {
        private object _minValue;
        private object _maxValue;

        /// <summary>
        /// Initilaizes a new instance of <B>Property.Is.In.RangeAtrribute</B>.
        /// </summary>
        /// <param name="minValue">The value which the property this is applied to should be greater than or equal to.</param>
        /// <param name="maxValue">The value which the property this is applied to should be lower than or equal to.</param>
        public RangeAttribute(object minValue, object maxValue)
        {
            _minValue = minValue;
            _maxValue = maxValue;
        }

        /// <summary>
        /// Performs validation on the property.
        /// </summary>
        /// <param name="property">The property that should be validated.</param>
        /// <returns><b>true</b> if <i>property</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateProperty(object property)
        {
            return Comparer.Compare(property, _minValue) >= 0 && Comparer.Compare(property, _maxValue) <= 0;
        }
    }
}
